<template>
  <div id="usermanage">
    <!--搜索表单开始-->
    <div class="search" style="margin-bottom: 20px">
      <el-input
        style="width: 240px"
        placeholder="请输入名称"
        v-model="name"
      ></el-input>
      <el-input
        style="width: 240px; margin-left: 5px"
        placeholder="请输入联系方式"
        v-model="phone"
      ></el-input>
      <el-button
        style="margin-left: 5px"
        type="primary"
        @click="(pageNum = 1), load()"
        ><i class="el-icon-search"></i> 搜索</el-button
      >
      <el-button style="margin-left: 5px" type="warning" @click="reset"
        ><i class="el-icon-refresh"></i> 重置</el-button
      >
    </div>
    <!-- 搜索表单结束 -->
    <!-- 表格开始 -->
    <div class="usertable">
      <el-table :data="tableData" stripe style="width: 100%">
        <el-table-column fixed prop="userName" label="账号" width="150">
        </el-table-column>
        <el-table-column prop="nikeName" label="姓名" width="90">
        </el-table-column>
        <el-table-column prop="gender" label="性别" width="80">
          <template slot-scope="scope">{{
            scope.row.gender | gender
          }}</template>
        </el-table-column>
        <el-table-column prop="phone" label="手机号" width="120">
        </el-table-column>
        <el-table-column prop="email" label="邮箱" width="120">
        </el-table-column>
        <el-table-column prop="createTime" label="创建时间" width="120">
          <template slot-scope="scope">
            {{ formatDate(scope.row.createTime) }}
          </template>
        </el-table-column>
        <el-table-column prop="updateTime" label="更新时间" width="120">
          <template slot-scope="scope">
            {{ formatDate(scope.row.updateTime) }}
          </template>
        </el-table-column>
        <el-table-column prop="avatarUrl" label="头像" width="300">
          <template slot-scope="scope">
            <el-image
              v-if="scope.row.avatarUrl != null"
              style="width: 100px; height: 100px"
              :src="scope.row.avatarUrl"
            ></el-image>
          </template>
        </el-table-column>

        <el-table-column label="操作" width="150">
          <template slot-scope="scope">
            <!-- <el-button
              size="mini"
              style="margin-right: 6px"
              @click="handleEdit(scope.$index, scope.row)"
              >编辑</el-button
            > -->
            <el-popconfirm
              title="确定删除该用户吗？"
              @confirm="handleDelete(scope.row)"
            >
              <el-button slot="reference" size="mini" type="danger"
                >删除</el-button
              >
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
      <div style="display: flex; justify-content: center">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageNum"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>
    </div>

    <!-- 表格结束 -->
  </div>
</template>


<script>
import request from "@/utils/request";
import Vue from "vue";
Vue.filter("gender", function (value) {
  return value === 1 ? "男" : "女";
});
export default {
  name: "HelloWorld",
  data() {
    return {
      total: 0,
      pageNum: 1,
      pageSize: 5,
      name: "",
      phone: "",
      tableData: [],
    };
  },
  created() {
    this.load();
  },
  methods: {
    load() {
      request
        .get("/user/page", {
          params: {
            pageNum: this.pageNum,
            pageSize: this.pageSize,
            name: this.name,
            phone: this.phone,
          },
        })
        .then((res) => {
          console.log(res);
          this.tableData = res.data.records;
          this.total = res.data.total;
        });
    },
    reset() {
      this.pageNum = 1;
      this.pageSize = 5;
      this.name = "";
      this.phone = "";
      this.load();
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pageSize = val;
      this.load();
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.pageNum = val;
      this.load();
    },
    handleDelete(user) {
      request.put("/user/delete", user).then((res) => {
        if (res.code === 200) {
          this.$notify.success("删除用户成功");
          this.load();
        } else {
          this.$notify.error(res.msg);
        }
      });
    },
    formatDate(timestamp) {
      const date = new Date(timestamp);
      const year = date.getFullYear();
      const month = date.getMonth() + 1;
      const day = date.getDate();
      const hour = date.getHours();
      const minute = date.getMinutes();
      const second = date.getSeconds();
      return `${year}-${month}-${day} ${hour}:${minute}:${second}`;
    },
  },
};
</script>


<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
.search {
  width: 675px;
  margin-left: 170px;
}
.el-button--primary {
  color: #fff;
  background-color: rgb(0, 184, 224);
  border-color: rgb(0, 184, 224);
}
/* 表格样式开始 */
.usertable {
  width: 1050px;
  /* margin-left: 350px; */
}
.demo-table-expand {
  font-size: 0;
}
.demo-table-expand label {
  width: 90px;
  color: #99a9bf;
}
.demo-table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 50%;
}
/* 表格样式结束 */
</style>